import { _decorator, Component, math, Node } from 'cc';
import { Tank } from './Tank';
import { Camp, Direction } from '../Define';
import { AudioManager } from '../manager/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Enemy')
export class Enemy extends Tank {
    public rotateTime: number = 1.0

    public shootTime: number = 0.5

    public curRotateTime: number = 0

    public curShootTime: number = 0

    onLoad() {
        super.onLoad()
        this.rotateTime += math.random() / 5
        this.shootTime += math.random() / 5
    }

    aiControl(dt: number) {
        this.curRotateTime += dt
        this.curShootTime += dt

        if (this.curRotateTime >= this.rotateTime) {
            this.curRotateTime = 0.0
            const dir = this.getDirection()
            if (dir == Direction.None) {
                this.stopMove()
            }
            else {
                this.setDirection(dir)
            }
        }

        if (this.curShootTime >= this.shootTime) {
            this.curShootTime = 0.0
            if (this.isShoot()) {
                this.shoot()
            }
        }
    }

    getDirection() {
        const dir = math.randomRangeInt(Direction.None, Direction.Down + 1)
        return dir
    }

    isShoot() {
        return math.randomRangeInt(0, 10) > 0
    }

    frameMove(dt: number) {
        this.aiControl(dt)
    }

    getCamp() {
        return Camp.Enemy
    }

    die() {
        AudioManager.inst.playOneShot("tank_bomb")
    }
}


